const fs = require('fs');
const path = require('path');

module.exports = function (pagePath, name, text) {
  const actionPath = pagePath + '/action';
  if (!fs.existsSync(actionPath)) {
    fs.mkdirSync(actionPath);
  }
  const actionName = `${name}Action`;
  const routeName = name.replace(/\w/, a => a.toLowerCase());

  const actionFile = actionPath + `/${actionName}.js`;
  if (!fs.existsSync(actionFile)) {
    const template = [];
    //template.push(`import { push } from 'react-router-redux';`);
    template.push(`import { ${name}API } from '../API';`);
    template.push(`import { ${actionName}Type } from '../const';`);
    template.push(``);
    // template.push(`/** 跳转到${text || 'XXXX'}页面 */`);
    // template.push(`export const goto${name} = () => {`);
    // template.push(`  return (dispatch) => {`);
    // template.push(`    dispatch(push('/${routeName}'));`);
    // template.push(`  };`);
    // template.push(`};`);
    template.push(``);
    fs.writeFileSync(actionFile, template.join('\n'));
  }

  const indexPath = actionPath + '/index.js';
  if (!fs.existsSync(indexPath)) {
    const template = [];
    template.push(`import * as ${actionName} from './${actionName}';`);
    template.push(``);
    template.push(`export { `);
    template.push(`  ${actionName}`);
    template.push(`};`);
    template.push(``);
    fs.writeFileSync(indexPath, template.join('\n'));
  }
}
